<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>	
<%@ include file="/pub/headmeta.jsp" %>
</head>
<body class="edit-bgr-color">
	<div class="right-head"><span class="right-head-font">新增角色</span></div>
	<div align="center" style="width: 98%">	
		<form id="addForm" method="post" enctype="multipart/form-data">			
			<table cellpadding="5" style="width: 600px; font-size: 12px;">
				<tr>
					<td width="35%" align="right">角色名称：</td>
					<td width="65%" align="left"><input type="text" name="name" id="name" style="width: 80%" required="required"/>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">角色描述：</td>
					<td align="left"><textarea name="description" id="description" style="width:80%" rows="5"></textarea>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>				
			</table>
			<br>
			<div align="left">分配权限：<input id="ckAll" type="checkbox" onclick="checkAll(this.checked,'cb');"/>全&nbsp;&nbsp;选</div>
			<hr width="100%"/>
			<table class="" id="MyTableList" border="0" cellpadding="0" cellspacing="5" style="width: 100%"></table>
			<div class="edit-line"></div>
			<input  type="button" class="button-1" value="新增" onclick="doAdd();"/>
			<input  type="button" class="button-1" value="重置" onclick="resetEx(this.form);"/>						
			<input  type="button" class="button-1" value="返回" onclick="back();"/>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
<!--
var power;
//自动调用初始化
$(document).ready(function(){	
	var url="${ctx}/role!getOps.action";	
	$.post(url, '', function callback(txt){
		var json = $.parseJSON(txt);
		if (json.code==0) {	
			power=json.info;	
			createTable(4);				
		}else{
			alert('获取所有模块-操作列表失败！');
		}
	});
});

//新增
function doAdd() {
	if ($("#name").val()== ""){
		alert('角色名称不能为空！');
		return;
	}
	if ($("#description").val() == ""){
		alert('角色描述不能为空！');
		return;
	}
	
	var form=$("#addForm");
	form.attr("action","${ctx}/role!add.action"); 
	form.submit();		
}

//返回
function back(){
	location.href="${ctx}/role!viewList.action";
}

//创建表数据 wrapNum-每行显示列数
function createTable(wrapNum){	
	//创建表
	var table=document.getElementById('MyTableList');
	for( var i=table.rows.length-1; i>=0; i--){
		table.deleteRow(i); 
	}
	var b=document.createElement("tbody");
	var p=0;
	var row;
	var w=100/wrapNum+"%";
	for(var att in power){	
		if(p%wrapNum==0){
			row=document.createElement("tr");	
			setTdStyle(row);b.appendChild(row);
		}	
		//内容准备
 		var content="<fieldset>";
 		content+="<legend><input type='checkbox' onclick=\"selectGroup(this.checked,'"+att+"');\" />"+att+"</legend>";
		for(var n in power[att]){
			var o=power[att][n];
			var sel=o.checked?"checked='checked'":"";
			content+="<p><input id='cb_"+o.operateId+"' name='cb' type='checkbox' "+sel+" value='"+o.operateId+"'/>"+o.operateName+"</p>";
		}			
 		content+="</fieldset>";	
 		//放入单元格
 		var c=document.createElement("td");	
		c.style.width=w;
		c.vAlign="top";
		c.innerHTML=content;
		row.appendChild(c);	
		p++;
	}
	table.appendChild(b);
}

//选择一组
function selectGroup(flag,att){
	for(var n in power[att]){
		var o=power[att][n];
		document.getElementById('cb_'+o.operateId).checked=flag;
	}
}
//-->
</script>
